<template>
  <div class="app-container">
    <!-- 顶部切换标签 -->
    <div class="tabs-container">
      <div
        v-for="(tab, index) in tabs"
        :key="index"
        class="tab"
        :class="{ active: currentTab === index }"
        @click="currentTab = index"
      >
        {{ tab }}
      </div>
      <button class="add-task-btn" @click="showAddTaskDialog = true">添加任务</button>
    </div>

    <!-- 任务添加弹窗 -->
    <div v-if="showAddTaskDialog" class="add-task-dialog">
      <div class="dialog-content">
        <h3>添加新任务</h3>
        <input v-model="newTaskTitle" placeholder="任务标题" />
        <textarea v-model="newTaskDescription" placeholder="任务描述" />
        <div class="dialog-buttons">
          <button @click="showAddTaskDialog = false">取消</button>
          <button @click="addTask">确认</button>
        </div>
      </div>
    </div>

    <!-- 任务展示区域 -->
    <div class="tasks-container">
      <div
        v-for="status in statuses"
        :key="status"
        class="status-column"
        @dragover="onDragOver($event)"
        @drop="onDrop(status)"
      >
        <h3 class="status-title">{{ status }}</h3>
        <div class="task-list">
          <div
            v-for="task in tasks[status]"
            :key="task.id"
            class="task-item"
            :style="{ backgroundColor: task.color }"
            draggable="true"
            @dragstart="onDragStart(task, status)"
          >
            <h4>{{ task.title }}</h4>
            <p>{{ task.description }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'

// 假数据
const tabs = ['日', '周', '月']
const currentTab = ref(0)

const tasks = ref({
  未开始: [
    { id: 1, title: '学习 Vue 3', description: '完成 Vue 3 官方文档阅读', color: '#FFCCCC' },
    { id: 2, title: '健身锻炼', description: '完成 30 分钟有氧运动', color: '#CCFFCC' },
    { id: 3, title: '阅读书籍', description: '阅读 30 页专业书籍', color: '#CCCCFF' },
    { id: 4, title: '项目开发', description: '完成新功能模块开发', color: '#FFFFCC' },
    { id: 5, title: '家务整理', description: '打扫房间和整理物品', color: '#FFCCFF' }
  ],
  执行中: [
    { id: 6, title: 'Vue 项目实践', description: '开发一个小型 Vue 项目', color: '#FFCCCC' },
    { id: 7, title: '跑步训练', description: '完成 5 公里长跑', color: '#CCFFCC' },
    { id: 8, title: '技术博客', description: '撰写一篇技术博客文章', color: '#CCCCFF' },
    { id: 9, title: '数据分析', description: '完成数据报表分析', color: '#FFFFCC' },
    { id: 10, title: '烹饪学习', description: '学习制作一道新菜品', color: '#FFCCFF' }
  ],
  已完成: [
    { id: 11, title: 'Vue 基础学习', description: '完成 Vue 基础教程学习', color: '#FFCCCC' },
    { id: 12, title: '游泳锻炼', description: '完成 1000 米游泳', color: '#CCFFCC' },
    { id: 13, title: '书籍阅读', description: '读完一本专业书籍', color: '#CCCCFF' },
    { id: 14, title: '项目测试', description: '完成项目单元测试', color: '#FFFFCC' },
    { id: 15, title: '园艺种植', description: '种植一盆新植物', color: '#FFCCFF' }
  ]
})

const statuses = Object.keys(tasks.value)
const draggedTask = ref(null)

const onDragStart = (task, status) => {
  draggedTask.value = {
    ...task,
    fromStatus: status
  }
}

const onDragOver = (e) => {
  e.preventDefault()
}

const onDrop = (status) => {
  if (draggedTask.value) {
    // 从原状态中移除任务
    const fromStatus = draggedTask.value.fromStatus
    tasks.value[fromStatus] = tasks.value[fromStatus].filter(
      (task) => task.id !== draggedTask.value.id
    )

    // 添加到新状态
    tasks.value[status].push(draggedTask.value)
    draggedTask.value = null
  }
}

const showAddTaskDialog = ref(false)
const newTaskTitle = ref('')
const newTaskDescription = ref('')

const addTask = () => {
  if (newTaskTitle.value.trim()) {
    const newTask = {
      id: Date.now(),
      title: newTaskTitle.value,
      description: newTaskDescription.value,
      color: '#FFCCCC'
    }
    tasks.value['未开始'].push(newTask)
    newTaskTitle.value = ''
    newTaskDescription.value = ''
    showAddTaskDialog.value = false
  }
}
</script>

<style scoped lang="scss">
.app-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: column;
}

.tabs-container {
  display: flex;
  overflow-x: auto;
  padding: 1rem;
  background-color: #f0f0f0;
  gap: 1rem;
  align-items: center;

  .tab {
    padding: 0.5rem 1rem;
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.3s;

    &:hover {
      background-color: #e0e0e0;
    }

    &.active {
      background-color: #42b983;
      color: white;
    }
  }

  .add-task-btn {
    padding: 0.5rem 1rem;
    background-color: #42b983;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s;

    &:hover {
      background-color: #33a06f;
    }
  }
}

.add-task-dialog {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;

  .dialog-content {
    background-color: white;
    padding: 2rem;
    border-radius: 8px;
    width: 400px;

    h3 {
      margin-top: 0;
    }

    input,
    textarea {
      width: 100%;
      padding: 0.5rem;
      margin-bottom: 1rem;
      border: 1px solid #ddd;
      border-radius: 4px;
    }

    textarea {
      height: 100px;
    }

    .dialog-buttons {
      display: flex;
      justify-content: flex-end;
      gap: 1rem;

      button {
        padding: 0.5rem 1rem;
        border: none;
        border-radius: 4px;
        cursor: pointer;

        &:first-child {
          background-color: #f0f0f0;
        }

        &:last-child {
          background-color: #42b983;
          color: white;
        }
      }
    }
  }
}

.tasks-container {
  flex: 1;
  display: flex;
  padding: 1rem;
  gap: 1rem;
  overflow-x: auto;
}

.status-column {
  flex: 1;
  min-width: 200px;
  background-color: #f8f8f8;
  border-radius: 8px;
  padding: 1rem;
  display: flex;
  flex-direction: column;

  .status-title {
    margin: 0 0 1rem 0;
    text-align: center;
    color: #333;
  }

  .task-list {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    overflow-y: auto; // 添加可滚动样式
  }
}

.task-item {
  padding: 1rem;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s;
  cursor: move;

  &:hover {
    transform: translateY(-2px);
  }

  h4 {
    margin: 0 0 0.5rem 0;
    color: #333;
  }

  p {
    margin: 0;
    color: #666;
    font-size: 0.9rem;
  }
}
</style>
